package com.vnp.vchat.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vnp.vchat.MainActivity;
import com.vnp.vchat.MainPanelActivity;
import com.vnp.vchat.R;
import com.vnp.vchat.Util.PreferenceHelper;
import com.vnp.vchat.Util.Utils;
import com.vnp.vchat.database.MySQLiteHelper;
import com.vnp.vchat.httpclient.HttpClientHelper;
import com.vnp.vchat.httpclient.MyJsonHttpResponseHandler;
import com.vnp.vchat.version2.InvitePhoneActivity;
import com.vnp.vchat.version2.KetnoiActivity;
import com.vnp.vchat.version2.TimkiemActivity;

public class KetnoiScr extends Fragment implements OnClickListener{

	TextView title;
	
	TextView titleTimkiem, titleInvite, titleCapnhat;
	
	RelativeLayout rlTimkiem, rlInvite, rlCapnhat;
	
	ProgressDialog progressDialog = null;
	
	ArrayList<String> list = new ArrayList<String>();
	MySQLiteHelper db;
	PreferenceHelper preferenceHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new MySQLiteHelper(getActivity());
		preferenceHelper = new PreferenceHelper(getActivity());
	}
	
	ViewGroup viewGroup;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(viewGroup==null) {
			viewGroup = (ViewGroup) inflater.inflate(R.layout.scr_ketnoi, container, false);
		} else {
			((ViewGroup) viewGroup.getParent()).removeView(viewGroup);
		}
		title = (TextView) viewGroup.findViewById(R.id.title);
		titleTimkiem = (TextView) viewGroup.findViewById(R.id.title_timkiem);
		titleInvite = (TextView) viewGroup.findViewById(R.id.title_moi_ban_be);
		titleCapnhat = (TextView) viewGroup.findViewById(R.id.title_capnhat_danhba);
		
		rlTimkiem = (RelativeLayout) viewGroup.findViewById(R.id.rl_timkiem);
		rlInvite = (RelativeLayout) viewGroup.findViewById(R.id.rl_moibanbe);
		rlCapnhat = (RelativeLayout) viewGroup.findViewById(R.id.rl_capnhat_danhba);
		
		init();
		setFont();
		return viewGroup;
	}
	
	void init(){
		title.setText("Kết nối");
		rlTimkiem.setOnClickListener(this);
		rlInvite.setOnClickListener(this);
		rlCapnhat.setOnClickListener(this);
	}
	
	void setFont(){
		title.setTypeface(Utils.getTypefaceBold(getActivity()));
		titleTimkiem.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleInvite.setTypeface(Utils.getTypefaceRegular(getActivity()));
		titleCapnhat.setTypeface(Utils.getTypefaceRegular(getActivity()));
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.rl_timkiem:
			gotoTimkiem();
			break;
			
		case R.id.rl_capnhat_danhba:
			showProgressDialog();
			UpdateContactAsyn asyn = new UpdateContactAsyn();
			asyn.execute("");
			break;
			
		case R.id.rl_moibanbe:
			gotoInvitePhone();
			break;

		default:
			break;
		}
	}
	
	void gotoTimkiem(){
		MainPanelActivity.onGoto = true;
		Intent t = new Intent(getActivity(), TimkiemActivity.class);
		startActivity(t);
		getActivity().overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
	}
	
	void gotoInvitePhone(){
		MainPanelActivity.onGoto = true;
		Intent t = new Intent(getActivity(), InvitePhoneActivity.class);
		startActivity(t);
		getActivity().overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
	}
	
	void showProgressDialog(){
		 progressDialog = new ProgressDialog(getActivity());
	     progressDialog.show();        
	     progressDialog.setContentView(R.layout.custom_progress_dialog);
	     //se ppdrá cerrar simplemente pulsando back
	     progressDialog.setCancelable(false);
	     
	     new Handler().postDelayed(new Runnable() {
	         public void run() {                
	             progressDialog.dismiss();         
	         }
	     }, 90000); 
	}
	
	private class UpdateContactAsyn extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			Utils.getContact(getActivity());
			sendContactHttp();
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			progressDialog.dismiss();
			getActivity().runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					Toast.makeText(getActivity(), "Cập nhật thành công", Toast.LENGTH_SHORT).show();
				}
			});
		}
		
		
		
	}
	
	void sendContactHttp(){
		
		
		list = db.getPhoneFromDB();
		
		JSONArray jsonArray = new JSONArray(list);
		
		/*for (int i = 0; i < objContacts.size(); i++) {
			jsonArray.put(objContacts.get(i).getJSONObject());
		}*/
		
		
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				Log.i("DATA", "Cap nhat danh ba loi");
			}

			@Override
			public void onSuccess(JSONObject re) {
				Log.i("DATA", "Cap nhat danh ba thanh cong :"+re.toString());
			}
			
		});
		
		
		httpClientHelper.sendContact(preferenceHelper.getAdminId(), jsonArray, preferenceHelper.getHashSetting());
	}

	
	
	
	
}
